package cn.lingyangwl.agile.food.core.controller.admin;

import cn.lingyangwl.agile.food.model.rqrs.goods.category.*;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.food.core.service.GoodsCategoryService;
import cn.lingyangwl.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品分类
 * 
 * @author shenguangyang
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("/goodsCategory")
public class GoodsCategoryController {
    @Resource
    private GoodsCategoryService categoryService;

    @ApiOperation(value = "通过id查找商品分类")
    @GetMapping("/listTree")
    @RamResource(action = "goods:category:query", name = "查询商品分类", type = ActionType.READ)
    public Resp<List<GoodsCategoryTreeResp>> listCategoryTree(GoodsCategoryTreeReq req) {
        return Resp.ok(categoryService.listCategoryTree(req));
    }

    @ApiOperation(value = "通过id查找商品分类")
    @GetMapping("/get")
    @RamResource(action = "goods:category:query", name = "查询商品分类", type = ActionType.READ)
    public Resp<GoodsCategoryResp> getCategory(@RequestParam(value = "id") Long id) {
        return Resp.ok(categoryService.getCategory(id));
    }
  
    @ApiOperation(value = "保存商品分类")
    @PostMapping("/save")
    @RamResource(action = "goods:category:save", name = "保存商品分类", type = ActionType.WRITE)
    public Resp<?> saveCategory(@Validated @RequestBody GoodsCategorySaveReq req) {
        return Resp.ok(categoryService.saveCategory(req));
    }
  
    @ApiOperation(value = "更新商品分类")
    @PostMapping("/update")
    @RamResource(action = "goods:category:update", name = "更新商品分类", type = ActionType.WRITE)
    public Resp<?> updateCategory(@Validated @RequestBody GoodsCategoryUpdateReq req) {
        return Resp.ok(categoryService.updateCategory(req));
    }
  
    @ApiOperation(value = "批量删除商品分类")
    @DeleteMapping("/batchDelete")
    @RamResource(action = "goods:category:delete", name = "删除商品分类", type = ActionType.WRITE)
    public Resp<?> batchDeleteCategory(@RequestParam(value = "ids") List<Long> ids) {
        return Resp.ok(categoryService.batchDeleteCategory(ids));
    }
  
    @ApiOperation(value = "删除商品分类")
    @DeleteMapping("/delete")
    @RamResource(action = "goods:category:delete", name = "删除商品分类", type = ActionType.WRITE)
    public Resp<?> deleteCategory(@RequestParam(value = "id") Long id) {
        return Resp.ok(categoryService.deleteCategory(id));
    }
}
